package com.inwavetech.os.tests.sync.jsonparser;

import com.inwavetech.os.entity.Os;
import com.inwavetech.os.entity.Product;
import com.inwavetech.os.entity.enummap.ProductDiagnosysMap;
import com.inwavetech.os.sync.jsonparser.InvalidEntityJsonException;
import com.inwavetech.os.sync.jsonparser.ProductJsonParser;
import com.inwavetech.os.tests.sync.mock.ProductJson;

import org.json.JSONException;

import java.text.ParseException;
import java.util.concurrent.Callable;

/**
 * Created by jgskin on 2/28/14.
 */
public class ProductJsonParserTest extends BaseJsonParserTest {

    private ProductDiagnosysMap diagnosysMap;

    @Override
    protected void setUp() throws Exception {
        diagnosysMap = new ProductDiagnosysMap();

        jsonParser = new ProductJsonParser(diagnosysMap, getDateHandler().getDateFormatter());

        anObject = new Product();

        aJson = new ProductJson(1, diagnosysMap, getDateHandler());
    }

    public void testInvalidJson_missingKeys() throws InvalidEntityJsonException, JSONException {
        // no missing keys
        parse();

        removeKeyAndTestException("id");
        removeKeyAndTestException("name");
        removeKeyAndTestException("identification");
        removeKeyAndTestException("factory_warranty");
        removeKeyAndTestException("service_warranty");
        removeKeyAndTestException("document_code");
        removeKeyAndTestException("maintenance_type");
        removeKeyAndTestException("hired_services");
        removeKeyAndTestException("maintenance_detail");
        removeKeyAndTestException("setup_detail");
        removeKeyAndTestException("diagnostic_detail");
        removeKeyAndTestException("diagnostic_id");
        removeKeyAndTestException("document_date");
    }

    public void testInvalidJson_invalidDocumentDate() throws InvalidEntityJsonException, JSONException {
        parse();

        aJson.put("document_date", "notvalid");
        assertInvalidEntityJsonException("document_date");
    }

    public void testDiagnosticId() throws Exception {
        assertEnumEntry(
                "diagnostic_id",
                false,
                diagnosysMap,
                Product.Diagnosys.ADJUST,
                new Callable<Enum>() {
                    @Override
                    public Enum call() throws Exception {
                        return ((Product) anObject).diagnosys;
                    }
                }
        );
    }

    public void testPopulateProduct() throws JSONException, InvalidEntityJsonException, ParseException {
        parse();

        Product product = (Product) anObject;

        // check the basic values
        assertEquals(0, product.id);
        assertEquals(aJson.get("id"), product.original_id);
        assertEquals(aJson.get("name"), product.name);
        assertEquals(aJson.get("identification"), product.identification);
        assertEquals(aJson.get("factory_warranty"), product.factory_warranty);
        assertEquals(aJson.get("service_warranty"), product.service_warranty);
        assertEquals(aJson.get("document_code"), product.document_code);
        assertEquals(aJson.get("maintenance_type"), product.maintenance_type);
        assertEquals(aJson.get("hired_services"), product.hired_services);
        assertEquals(aJson.get("maintenance_detail"), product.maintenance_detail);
        assertEquals(aJson.get("setup_detail"), product.setup_detail);
        assertEquals(aJson.get("diagnostic_detail"), product.diagnosys_detail);

        // check the special values
        assertEquals(Product.Diagnosys.ADJUST, product.diagnosys);
        assertEquals(getDateHandler().getDate().getTime(), product.document_date.getTime());
    }
}
